home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_6 / diffext.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  928 b   |  29 lines  |  [MATF/MATL]

  1. function [D,err,relerr,n] = diffext(f,x,delta,toler)
  2. % [D,err,relerr,n] = diffext(f,x,delta,toler)
  3. % Numerical approximation for f'(x).
  4. % The method is Richardson`s extrapolation.
  5. % f is the function, input.
  6. % x is differentiation point, input.
  7. % delta is the error goal, input.
  8. % toler is the relative error goal, input.
  9. % D is the matrix of approximate derivatives, output.
  10. % error is the error bound, output.
  11. % relerr is the relative error bound, output.
  12. % n is the coordinate of the "best approximation", output.
  13. err = 1;
  14. relerr = 1;
  15. h = 1;
  16. j = 1;
  17. D(0+1,0+1) = (feval(f,x+h) - feval(f,x-h))/(2*h);
  18. while relerr>toler & err>delta & j<12
  19.   h = h/2;
  20.   D(j+1,0+1) = (feval(f,x+h) - feval(f,x-h))/(2*h);
  21.   for k = 1:j,
  22.     D(j+1,k+1) = D(j+1,k-1+1) + (D(j+1,k-1+1)-D(j-1+1,k-1+1))/(4^k -1);
  23.   end
  24.   err = abs(D(j+1,j+1)-D(j-1+1,j-1+1));
  25.   relerr = 2*err/(abs(D(j+1,j+1))+abs(D(j-1+1,j-1+1))+eps);
  26.   j = j+1;
  27. end
  28. [n n] = size(D);
  29.